package com.dk.service.impl;

import com.dk.mapper.RoleMenuMapper;
import com.dk.mapper.TRoleMapper;
import com.dk.pojo.RoleMenuExample;
import com.dk.pojo.TRole;
import com.dk.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private TRoleMapper tRoleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;


    @Override
    public List<TRole> getInfo() {

        return tRoleMapper.selectByExample(null);
    }

    @Override
    public boolean deleteInfo(Integer roleId) {

        int i = tRoleMapper.deleteByPrimaryKey(roleId);

        RoleMenuExample re = new RoleMenuExample();

        RoleMenuExample.Criteria criteria = re.createCriteria();

        criteria.andRoleIdEqualTo(roleId);

        int i2 = roleMenuMapper.deleteByExample(re);

        return true;
    }

    @Override
    public boolean addInfo(TRole tRole,Integer[] mids) {

        try {

            Integer integer = tRoleMapper.insertInfo(tRole);

            Integer roleId= tRole.getRoleId();

            int i2 = roleMenuMapper.insertBach(roleId, mids);

            return true;
        } catch (Exception e) {

            e.printStackTrace();

            return false;
        }
    }

    @Override
    public TRole toUpdate(Integer roleId) {

        TRole role = tRoleMapper.selectByPrimaryKey(roleId);

        return role;
    }

    @Override
    public boolean updateInfo(TRole tRole, Integer[] mids) {

        int i = tRoleMapper.updateByPrimaryKey(tRole);
        //删除中间表然后再增加
        RoleMenuExample re = new RoleMenuExample();

        RoleMenuExample.Criteria criteria = re.createCriteria();

        criteria.andRoleIdEqualTo(tRole.getRoleId());

        int i2 = roleMenuMapper.deleteByExample(re);

        int i3 =roleMenuMapper.insertBach(tRole.getRoleId(),mids);

        return true;
    }
}
